package com.cssy.authority.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.byd.acegiext.domain.User;
import com.cssy.authority.entity.ScUserOrganizationDO;
import com.cssy.authority.entity.SysMenuDO;
import com.cssy.authority.service.IMenuService;
import com.cssy.authority.service.IScMenuRoleOrganizationService;
import com.plat.util.SessionUtil;

@Controller
@RequestMapping("/views")
public class PageController {
	private static final String INDEX = "/index";
	
	private static final String NEWINDEX = "/menu/index";
	
	@Resource(name="scUsermenuPlaceService")
	private IScMenuRoleOrganizationService scUsermenuPlaceService;
	
	 @Resource(name = "menuService")
	 private IMenuService menuService;
	
	@RequestMapping("/index")
	public String index(HttpServletRequest request, Model model) throws Exception {
		/*Authentication auth = */SecurityContextHolder.getContext().getAuthentication();
		model.addAttribute("meun",meun(request));
		return INDEX;
	}
	
	public String meun(HttpServletRequest request){
		User user=SessionUtil.getUser(request);
		List<ScUserOrganizationDO> sc=scUsermenuPlaceService.findByUserId(user.getId()+"");
		List<SysMenuDO> smlist=new ArrayList<SysMenuDO>();
		if(sc.size()>0){
			for(int i=0;i <sc.size();i++){
				SysMenuDO sm=menuService.findListById(sc.get(i).getOrganizationid()+"");
				if(sm.getId()!=null){
					smlist.add(sm);
				}
			}
		}
		boolean fa=true;
		StringBuffer str=new StringBuffer();
		if(smlist.size()>0){
			str.append("{");
			for(int i=0;i<smlist.size();i++){
				if("业务管理".equals(smlist.get(i).getName())&&"0".equals(smlist.get(i).getUrl())){
					if(fa){
						str.append(" tab1 :").append("{ type : \"tree\",").append("text : \"").append(smlist.get(i).getName()).append("\",").append("children : [");
					}else{
						str.append(", tab1 :").append("{ type : \"tree\",").append("text : \"").append(smlist.get(i).getName()).append("\",").append("children : [");
					}
					fa=false;
					boolean fa2=true;
					for(int n=0;n<smlist.size();n++){
						if(smlist.get(i).getId().equals(smlist.get(n).getMenuid())){
							if(fa2){
								str.append("{ text :").append("\'").append(smlist.get(n).getName()).append("\',url : \"").append(smlist.get(n).getUrl()).append("\"");
							}else{
								str.append(", { text :").append("\'").append(smlist.get(n).getName()).append("\',url : \"").append(smlist.get(n).getUrl()).append("\"");
							}
							fa2=false;
							boolean fa1=true;
							int b=0;
							for(int k=0;k<smlist.size();k++){
								if(smlist.get(n).getId().equals(smlist.get(k).getMenuid())){
									fa1=false;
									if(b==0){
										str.append(",isexpand:true,icon:treeIcon1, children: [").append("{ text :").append("\'").append(smlist.get(k).getName()).append("\',").append("url : ").append("\"").append(smlist.get(k).getUrl()).append("\",icon:treeIcon2 }");
									}else{
										str.append(",{ text :").append("\'").append(smlist.get(k).getName()).append("\',").append("url : ").append("\"").append(smlist.get(k).getUrl()).append("\" ,icon:treeIcon2}");
									}
									b++;
								}
							}
							if(fa1){
								str.append("}");
							}else{
								str.append("]}");
							}							
						}
					}
					str.append(" ]}");
				}
				if("个人管理".equals(smlist.get(i).getName())&&"1".equals(smlist.get(i).getUrl())){
					if(fa){
						str.append(" tab2 :").append("{ type : \"tree\",").append("text : \"").append(smlist.get(i).getName()).append("\",").append("children : [");
					}else{
						str.append(", tab2 :").append("{ type : \"tree\",").append("text : \"").append(smlist.get(i).getName()).append("\",").append("children : [");
					}
					fa=false;
					boolean fa2=true;
					for(int n=0;n<smlist.size();n++){
						if(smlist.get(i).getId().equals(smlist.get(n).getMenuid())){
							if(fa2){
								str.append("{ text :").append("\'").append(smlist.get(n).getName()).append("\',url : \"").append(smlist.get(n).getUrl()).append("\"");
							}else{
								str.append(", { text :").append("\'").append(smlist.get(n).getName()).append("\',url : \"").append(smlist.get(n).getUrl()).append("\"");
							}
							fa2=false;
							boolean fa1=true;
							int b=0;
							for(int k=0;k<smlist.size();k++){
								if(smlist.get(n).getId().equals(smlist.get(k).getMenuid())){
									fa1=false;
									if(b==0){
										str.append(",isexpand:true, children: [").append("{ text :").append("\'").append(smlist.get(k).getName()).append("\',").append("url : ").append("\"").append(smlist.get(k).getUrl()).append("\" }");
									}else{
										str.append(",{ text :").append("\'").append(smlist.get(k).getName()).append("\',").append("url : ").append("\"").append(smlist.get(k).getUrl()).append("\" }");
									}
									b++;
								}
							}
							if(fa1){
								str.append("}");
							}else{
								str.append("]}");
							}							
						}
					}
					str.append(" ]}");
				}
				if("权限管理".equals(smlist.get(i).getName())&&"3".equals(smlist.get(i).getUrl())){
					if(fa){
						str.append(" tab3 :").append("{ type : \"tree\",").append("text : \"").append(smlist.get(i).getName()).append("\",").append("children : [");
					}else{
						str.append(", tab3 :").append("{ type : \"tree\",").append("text : \"").append(smlist.get(i).getName()).append("\",").append("children : [");
					}
					fa=false;
					boolean fa2=true;
					for(int n=0;n<smlist.size();n++){
						if(smlist.get(i).getId().equals(smlist.get(n).getMenuid())){
							if(fa2){
								str.append("{ text :").append("\'").append(smlist.get(n).getName()).append("\',url : \"").append(smlist.get(n).getUrl()).append("\"");
							}else{
								str.append(", { text :").append("\'").append(smlist.get(n).getName()).append("\',url : \"").append(smlist.get(n).getUrl()).append("\"");
							}
							fa2=false;
							boolean fa1=true;
							int b=0;
							for(int k=0;k<smlist.size();k++){
								if(smlist.get(n).getId().equals(smlist.get(k).getMenuid())){
									fa1=false;
									if(b==0){
										str.append(",isexpand:true, children: [").append("{ text :").append("\'").append(smlist.get(k).getName()).append("\',").append("url : ").append("\"").append(smlist.get(k).getUrl()).append("\" }");
									}else{
										str.append(",{ text :").append("\'").append(smlist.get(k).getName()).append("\',").append("url : ").append("\"").append(smlist.get(k).getUrl()).append("\" }");
									}
									b++;
								}
							}
							if(fa1){
								str.append("}");
							}else{
								str.append("]}");
							}							
						}
					}
					str.append(" ]}");
				}
			}
			str.append("}");
		}
		
		return str.toString() ;
	}
	
	@RequestMapping("/newindex")
	public String newIndex(HttpServletRequest request, Model model) throws Exception {
//		SecurityContextHolder.getContext().getAuthentication();
//		model.addAttribute("meun",meun(request));
		return NEWINDEX;
	}
}
